package com.iris.live.services.services.impl;

import com.iris.live.services.common.JDBCILogUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.ILogInfoRepository;
import com.iris.live.services.services.ILogInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/31.
 */
@Service
public class ILogInfoServiceImpl implements ILogInfoService {

    private static Logger logger = LoggerFactory.getLogger(ILogInfoServiceImpl.class);

    @Autowired
    private ILogInfoRepository iLogInfoRepository;

    @Override
    public boolean getILogScInfo(String permit, List<GetILogScInfo> list) {
        boolean flag = false;
        List<String> listUserName = new ArrayList<>();
        List listUserInfo = null;
        List<Map<String, String>> listParam = new ArrayList();
        for (GetILogScInfo getILogScInfo: list) {
            listUserName.add(getILogScInfo.id);
        }
        if (listUserName != null && !listUserName.isEmpty()) {
            listUserInfo = iLogInfoRepository.getLiveScInfo(permit, listUserName);
        }
        if (listUserInfo != null && !listUserInfo.isEmpty()) {
            for (Object o : listUserInfo) {
                Map mapUserInfo = (Map) o, mapValue = new HashMap();
                for (GetILogScInfo getILogScInfo : list) {
                    if (mapUserInfo.get("userName") != null && mapUserInfo.get("userName").equals(getILogScInfo.id)
                            && !StringUtils.isNullOrBlank(getILogScInfo.id) && mapUserInfo.get("userEmail") != null) {
                        mapValue.put("id", getILogScInfo.userName);
                        mapValue.put("userEmail", mapUserInfo.get("userEmail").toString());
                        listParam.add(mapValue);
                    }
                }
            }
        }
        if (listParam != null && !listParam.isEmpty()) {
            String permitLower = permit.toLowerCase();
            String tableName = permitLower + "销售顾问";
            List listTableName = iLogInfoRepository.getILogtableName();
            List<String> listSql = new ArrayList<>();

            updateScInfoAboutSales(permit, tableName, listTableName, listParam, listSql);
            if (listSql != null && !listSql.isEmpty()) {
                updateScInfoAboutReceptionRecord(permit, tableName, listTableName, listParam, listSql);
                updateScInfoAboutWorkStatistics(permit, tableName, listTableName, listParam, listSql);
                updateScInfoAboutSaleTarget(permit, tableName, listTableName, listParam, listSql);
                updateKeepVariable(permit, tableName, listTableName, listParam, listSql);
                flag = JDBCILogUtils.runUpdateSqls(listSql);
            }
        }
        return flag;
    }

//    //将  ilog中指定 销售顾问  表中的  电子邮箱 修改为 live数据库中相匹配 的用户名，即 电子邮箱
    private void updateScEmailInfoAboutSales(String permit, String tableName, List list, List<Map<String, String>> listSc) {
        if (list != null && !list.isEmpty()) {
            //获取到ilog中相应的 销售顾问 表
            Map<String, String> mapTableName = getILogTableName(tableName, list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map: listSc) {
                    iLogInfoRepository.updateEmailInfoAboutSales(permit, map.get("userEmail"), map.get("id"), key);
                }
            }
        }
    }

    private void updateScInfoAboutSales(String permit, String tableName, List list, List<Map<String, String>> listSc, List<String> listSql){
        if (list != null && !list.isEmpty()) {
            //获取到ilog中相应的 销售顾问 表
            Map<String, String> mapTableName = getILogTableName(tableName, list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map: listSc) {
                    String sql = "UPDATE " + key + " a SET a.`用户名` = '" + map.get("userEmail") + "' WHERE a.`用户名` = '" + map.get("id") + "';";
                    listSql.add(sql);
                }
            }
        }
    }

    private void updateScInfoAboutReceptionRecord(String permit, String tableName, List list, List<Map<String, String>> listSc, List<String> listSql) {
        if (list != null && !list.isEmpty()) {
            Map<String, String> mapTableName = getILogTableName("接待记录", list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map : listSc) {
                    String sql1 = "UPDATE " + key + " a SET a.`销售顾问` = '" + map.get("userEmail") + "' WHERE a.`经销店` = '" + permit + "' AND a.`销售顾问` = '" + map.get("id") + "';",
                            sql2 = "UPDATE " + key + " a SET a.`是否指定销售顾问` = '" + map.get("userEmail") + "' WHERE a.`经销店` = '" + permit + "' AND a.`是否指定销售顾问` = '" + map.get("id") + "';";
                    listSql.add(sql1);
                    listSql.add(sql2);
                }
            }
        }
    }

    private void updateScInfoAboutWorkStatistics(String permit, String tableName, List list, List<Map<String, String>> listSc, List listSql) {
        if (list != null && !list.isEmpty()) {
            Map<String, String> mapTableName = getILogTableName("工作统计v2", list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map : listSc) {
                    String sql = "UPDATE " + key + " a SET a.`销售顾问` = '" + map.get("userEmail") + "' WHERE a.`经销店` = '" + permit + "' AND a.`销售顾问` = '" + map.get("id") + "';";
                    listSql.add(sql);
                }
            }
        }
    }

    private void updateScInfoAboutSaleTarget(String permit, String tableName, List list, List<Map<String, String>> listSc, List<String> listSql) {
        if (list != null && !list.isEmpty()) {
            Map<String, String> mapTableName = getILogTableName("销售顾问目标", list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map : listSc) {
                    String sql = "UPDATE " + key + " a SET a.`销售顾问` = '" + map.get("userEmail") + "' WHERE a.`经销商代码` = '" + permit + "' AND a.`销售顾问` = '" + map.get("id") + "';";
                    listSql.add(sql);
                }
            }
        }
    }

    private void updateKeepVariable(String permit, String tableName, List list, List<Map<String, String>> listSc, List<String> listSql) {
        if (list != null && !list.isEmpty()) {
            Map<String, String> mapTableName = getILogTableName("年留存变量", list);
            for (String key : mapTableName.keySet()) {
                for (Map<String, String> map : listSc) {
                    String sql = "UPDATE " + key + " a SET a.`销售顾问` = '" + map.get("userEmail") + "' WHERE a.`经销店` = '" + permit + "' AND a.`销售顾问` = '" + map.get("id") + "';";
                    listSql.add(sql);
                }
            }
        }
    }

    private Map<String, String> getILogTableName(String param, List list) {
        Map<String, String> values = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map map = (Map) o;
                String tableName = map.get("TABLE_NAME") == null ? "" : map.get("TABLE_NAME").toString();
                if (tableName != null && tableName.indexOf("销售顾问") != -1 && tableName.indexOf("销售顾问目标") == -1) {
                    if (tableName.equalsIgnoreCase(param)) {
                        values.put(tableName, tableName);
                    }
                } else {
                    if (tableName.indexOf(param) != -1) {
                        values.put(tableName, tableName);
                    }
                }
            }
        }
        return values;
    }
}
